package com.example.one;

import java.util.*;
import java.util.stream.Collectors;

public class MainApp {

        private Trader raoul = new Trader("Raoul", "Cambridge");
//        private Trader raoul = new Trader("Raoul", "Cambridge");
//        private Trader raoul = new Trader("Raoul", "Cambridge");
//        private Trader raoul = new Trader("Raoul", "Cambridge");
        private Trader mario = new Trader("Mario", "Milan");
        private Trader alan = new Trader("Alan", "Cambridge");
        private Trader brian = new Trader("Brian", "Cambridge");

        private List<Transaction> transactionList = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        //TODO ... 待添加的方法...

    void text_1 () {
        List<Transaction> list = transactionList.stream()
                .filter(item ->
                        item.getYear().equals(2011))
                .sorted((e1, e2) ->
                        e2.getValue().compareTo(e1.getValue()))
                .collect(Collectors.toList());
        System.out.println(list);
    }

    //找出`2011`年发生的所有交易，并按交易额排序（从高到低）。
    void test_1(){
        List<Transaction> stream = transactionList.stream()
                .filter(item -> item.getYear().equals(2011))
                .sorted((a,b) -> a.getYear().compareTo(b.getYear()))
                .collect(Collectors.toList());
        System.out.println(stream);
    }

    void text_2(){
        List<String > list = transactionList.stream()
                .map(Transaction :: getTrader)
                .map(Trader::getCity)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(list);
    }

    //交易员都在哪些不同的城市工作过？

    /**
     * 先找出交易员
     */
    void test_2(){
        List<String> stream = transactionList.stream()
                .map(Transaction::getTrader)
                .map(Trader::getCity)
                .collect(Collectors.toList());
        System.out.println(stream);
    }


    void text_3(){
            List<Trader> list = transactionList.stream()
                .map(Transaction :: getTrader)
                .distinct()
                .sorted((o1,o2) ->
                        o1.getName().compareTo(o2.getName()))
                .collect(Collectors.toList());

        System.out.println(list);
    }

    //查找所有来自`Cambridge`的交易员，并按姓名排序。
    void test_3(){
        List<Trader> stream = transactionList.stream()
                .map(Transaction :: getTrader)
                .sorted((a,b) -> a.getName().compareTo(b.getName()))
                .collect(Collectors.toList());
        System.out.println(stream);
    }

    void text_4(){
        List<String> list= transactionList.stream()
                .map(Transaction :: getTrader)
                .map(Trader :: getName)
                .distinct()
                .sorted(String :: compareTo)
                .collect(Collectors.toList());
        System.out.println(list);
    }

    //返回所有交易员的姓名字符串，按字母顺序排序。
    void test_4(){
        List<String> stream = transactionList.stream()
                .map(Transaction::getTrader)
                .map(Trader::getName)
                .sorted(String::compareTo)
                .collect(Collectors.toList());
        System.out.println(stream);
    }

    void text_5(){
        List<Trader> list = transactionList.stream()
                .map(Transaction :: getTrader)
                .distinct()
                .filter(item ->
                        item.getCity().equals("Milan"))
                .collect(Collectors.toList());
        System.out.println(list);
    }

    //有没有交易员在`Milan`工作
    void test_5(){
        List<Trader> stream = transactionList.stream()
                .map(Transaction::getTrader)
                .filter(item -> item.getCity().equals("Milan"))
                .collect(Collectors.toList());
        System.out.println(stream);
    }


    void text_6(){
        Integer list = transactionList.stream()
                .filter(item ->
                        item.getTrader().getCity().equals("Cambridge"))
                .collect(Collectors.summingInt(Transaction :: getValue));
        System.out.println(list);
    }

    //打印生活在`Cambridge`的交易员的所有交易额。
    void test_6(){
        Integer stream = transactionList.stream()
                .filter(item ->
                        item.getTrader().getCity().equals("Cambridge"))
                .mapToInt(Transaction::getValue).sum();
        System.out.println(stream);
    }

    void text_7(){
        Optional<Integer> max = transactionList.stream()
                .map(Transaction :: getValue)
                .collect(Collectors.maxBy(Integer :: compareTo));
        System.out.println(max);
    }

    void text_8(){
        Optional<Integer> min = transactionList.stream()
                .map(Transaction :: getValue)
                .collect(Collectors.minBy(Integer::compareTo));
        System.out.println(min);
    }


    public static void main(String[] args) {
        MainApp mainApp = new MainApp();
//        mainApp.text_1();
//        mainApp.text_2();
//        mainApp.text_3();
//        mainApp.text_4();
//        mainApp.text_5();
        mainApp.text_6();
//        mainApp.text_7();
//        mainApp.text_8();

        //mainApp.test_1();
        //mainApp.test_2();
        //mainApp.test_3();
        //mainApp.test_4();
        //mainApp.text_5();
        mainApp.test_6();
    }

}
